<?php

namespace app\common\model\caijiatong;

use think\Model;
use think\Db;

/**
 * 材价通用户表
 */
class User extends Model
{
    protected $connection = 'database.db_second';//连接配置
    protected $table = 'fa_user';//连接配置
    public function getUserInfo($mobile)
    {
        $where = [
            'mobile' => $mobile,
            'status' => 'normal'
        ];
        $info = $this->where($where)->find();
        return $info;
    }

    /**
     * 变更会员积分
     * @param int    $score   积分
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function score($score, $mobile, $memo, $type = 0)
    {
        $user = self::where(['mobile' => $mobile,'status' => 'normal'])->find();;
        if ($user && $score != 0) {
            $before = $user->score;
            $after = $user->score + $score;
            $update = [
                'score' => $after
            ];
            if ($score > 0) {
                $update['totalscore'] = $user->totalscore + $score;
            }
            //更新会员信息
            $user->save($update);
            //写入日志
            Db::connect('database.db_second')->name('user_score_log')->insert(['user_id' => $user->id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo, 'type' => $type, 'createtime' => time()]);
        }
    }

}
